<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'m8:order:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="50%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasFyBasTeamForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed, h } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { TestTree, testTreeSave, testTreeForm, testTreeTreeData } from '/@/api/test/testTree';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('test.testTree');
  const { showMessage } = useMessage();
  const record = ref<TestTree>({} as TestTree);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增数据') : t('编辑数据'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('订单号'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('设计师'),
      field: 'name1',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('加工单位'),
      field: 'name2',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('加工名称'),
      field: 'name3',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('图纸数量'),
      field: 'code',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
      rules: [
        { required: true },
        { pattern: /^[a-zA-Z0-9_]*$/, message: t('请输入字母数字下划线') },
      ],
    },
    {
      label: t('下图日期'),
      field: 'testDatetime1',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('交付日期'),
      field: 'testDatetime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('紧急程度'),
      field: 'testRadio',
      component: 'RadioGroup',
      componentProps: {
        dictType: 'sys_menu_type',
      },
    },
    {
      label: t('交付数量'),
      field: 'testDataChildList',
      component: 'Input',
      // slot: 'testDataChildList',
      // suffix: h('Select', { class: 'ml-4 text-gray-500'}, '123'),
    },

    {
      label: t('备注'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      // colProps: { lg: 24, md: 24 },
    },

    // {
    //   label: t('上级数据'),
    //   field: 'parentCode',
    //   fieldLabel: 'parentName',
    //   component: 'TreeSelect',
    //   componentProps: {
    //     allowClear: true,
    //   },
    // },
    // {
    //   label: t('节点编码'),
    //   field: 'treeCode',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 64,
    //   },
    //   rules: [
    //     { required: true },
    //     { pattern: /^[a-zA-Z0-9_]*$/, message: t('请输入字母数字下划线') },
    //   ],
    // },
    // {
    //   label: t('排序号'),
    //   field: 'treeSort',
    //   helpMessage: '升序',
    //   component: 'InputNumber',
    //   componentProps: {
    //     maxlength: 10,
    //   },
    //   defaultValue: '30',
    //   required: true,
    // },
    // {
    //   label: t('节点名称'),
    //   field: 'treeName',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 200,
    //   },
    //   required: true,
    // },
    // {
    //   label: t('备注信息'),
    //   field: 'remarks',
    //   component: 'InputTextArea',
    //   componentProps: {
    //     maxlength: 500,
    //   },
    //   colProps: { lg: 24, md: 24 },
    // },
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await testTreeForm(data);
    record.value = (res.testTree || {}) as TestTree;
    if (data.parentCode && data.parentName) {
      record.value.parentCode = data.parentCode;
      record.value.parentName = data.parentName;
    }
    setFieldsValue(record.value);
    updateSchema([
      {
        field: 'parentCode',
        componentProps: {
          api: testTreeTreeData,
          params: {
            excludeCode: record.value.id,
            isShowRawName: true,
          },
        },
      },
      {
        field: 'code',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
    ]);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        // treeCode: record.value.treeCode,
        code: record.value.code,
      };
      // console.log('submit', params, data, record);
      const res = await testTreeSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
